﻿using System;
using System.Collections.Generic;

public partial class UserServiceImpl : IUserService
{
    private List<User> _users;

    public UserServiceImpl()
    {
        _users = new List<User>();
    }

    public bool Register(string username, string password)
    {
        // 检查用户名是否已存在
        foreach (User user in _users)
        {
            if (user.Username == username)
            {
                return false;
            }
        }

        // 添加新用户
        _users.Add(new User(username, password));
        return true;
    }

    public bool Login(string username, string password)
    {
        // 验证用户名和密码
        foreach (User user in _users)
        {
            if (user.Username == username && user.Password == password)
            {
                return true;
            }
        }
        return false;
    }

    public void Run()
    {
        while (true)
        {
            Console.WriteLine("请选择操作：1.注册 2.登录 3.退出");
            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    Console.WriteLine("请输入用户名：");
                    string regUser = Console.ReadLine();
                    Console.WriteLine("请输入密码：");
                    string regPwd = Console.ReadLine();

                    if (Register(regUser, regPwd))
                    {
                        Console.WriteLine("注册成功！");
                    }
                    else
                    {
                        Console.WriteLine("用户名已存在，注册失败！");
                    }
                    break;

                case "2":
                    Console.WriteLine("请输入用户名：");
                    string loginUser = Console.ReadLine();
                    Console.WriteLine("请输入密码：");
                    string loginPwd = Console.ReadLine();

                    if (Login(loginUser, loginPwd))
                    {
                        Console.WriteLine("登录成功！");
                    }
                    else
                    {
                        Console.WriteLine("用户名或密码错误，登录失败！");
                    }
                    break;

                case "3":
                    Console.WriteLine("程序退出！");
                    return;

                default:
                    Console.WriteLine("无效选择，请重新输入！");
                    break;
            }
        }
    }
}